﻿using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Articles;
using Ue.Qcdz.Domain.Tags;

namespace Ue.Qcdz.Repositories.Articles
{
    public class ArticleRepository : SqlServerRepository<Domain.Articles.Article>, IArticleRepository
    {

        public ArticleRepository()
            : this(null) { }

        public ArticleRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork) { }


        #region :SqlServerRepository
        protected override string GetDataTableName()
        {
            return "Articles";
        }

        protected override string GetKeyFieldName()
        {
            return "id";
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[] {
             ArticleFactory.FieldNames.EntityKey,
              ArticleFactory.FieldNames.Title,
               ArticleFactory.FieldNames.SubTitle,
                ArticleFactory.FieldNames.Content,
                 ArticleFactory.FieldNames.ImageUrl,
                  ArticleFactory.FieldNames.Keyword,
                   ArticleFactory.FieldNames.TagId,
                    ArticleFactory.FieldNames.Reference,
                     ArticleFactory.FieldNames.Summary,                      
                      ArticleFactory.FieldNames.ClickCount,
                       ArticleFactory.FieldNames.Editor,
                         ArticleFactory.FieldNames.Remark,
                          ArticleFactory.FieldNames.IsTop,
                           ArticleFactory.FieldNames.Status,
                            ArticleFactory.FieldNames.Ver,
                             ArticleFactory.FieldNames.DisplayOrder,                            
                              ArticleFactory.FieldNames.CreateOn,
                               ArticleFactory.FieldNames.TitleColor,
                               ArticleFactory.FieldNames.IsStrong,
                               ArticleFactory.FieldNames.Relevance,
                               ArticleFactory.FieldNames.IsFocus
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[] {
              ArticleFactory.FieldNames.Title,
               ArticleFactory.FieldNames.SubTitle,
                ArticleFactory.FieldNames.Content,
                 ArticleFactory.FieldNames.ImageUrl,
                  ArticleFactory.FieldNames.Keyword,
                   ArticleFactory.FieldNames.TagId,
                    ArticleFactory.FieldNames.Reference,
                     ArticleFactory.FieldNames.Summary, 
                     ArticleFactory.FieldNames.ClickCount,
                      ArticleFactory.FieldNames.Editor,
                       ArticleFactory.FieldNames.Remark,
                        ArticleFactory.FieldNames.IsTop,
                         ArticleFactory.FieldNames.Status,
                          ArticleFactory.FieldNames.Ver,
                           ArticleFactory.FieldNames.DisplayOrder,
                            ArticleFactory.FieldNames.TitleColor,
                             ArticleFactory.FieldNames.IsStrong,
                             ArticleFactory.FieldNames.Relevance,
                              ArticleFactory.FieldNames.IsFocus

            };
        }

        protected override System.Data.IDataParameter[] BuildParameters(Domain.Articles.Article entity)
        {
            return new IDataParameter[]{
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.EntityKey,entity.Key),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Title,entity.Title??""),              
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Summary,entity.Summary??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.SubTitle,entity.SubTitle??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Status,entity.Status),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Remark,entity.Remark??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Reference,entity.Reference??""),  
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.TagId,entity.Tag.Key),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Keyword,entity.Keyword??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Content,entity.Content??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.IsTop,entity.IsTop),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.ImageUrl,entity.ImageUrl??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Editor,entity.Editor??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.ClickCount,entity.ClickCount),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.DisplayOrder,entity.DisplayOrder), 
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Ver,entity.Ver??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.CreateOn,entity.CreateOn),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.TitleColor,entity.TitleColor??""),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.IsStrong,entity.IsStrong),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Relevance,entity.Relevance),
              this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.IsFocus,entity.IsFocus)

            };
        }


        protected override IDictionary<string, SqlServerRepositoryBase<Domain.Articles.Article>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            var callBacks = new Dictionary<string, SqlServerRepository<Article>.ChildDataHandler>();
            callBacks.Add(ArticleFactory.FieldNames.TagId, delegate(Article article, object fieldValue)
            {
                ArticleProxy articleProxy = article as ArticleProxy;
                if (articleProxy != null)
                {
                    ITagRepository tagRepository = RepositoryFactory.GetRepository<ITagRepository, Tag>();

                    articleProxy.Tag = tagRepository[fieldValue];
                }
            });

            return callBacks;
        }
        #endregion

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            if (dataQuery is IArticleDataQuery)
            {
                return new ArticleDataQueryWhereClause(dataQuery as IArticleDataQuery);
            }

            return null;
        }

        #region IArticleRepository 成员

        public Article GetArticle(string title)
        {
            string sql = "SELECT TOP 1 * FROM Articles WHERE Title=@Title";
            var parameter = new IDataParameter[]
                                             {
                                                 this.RepositoryDriver.BuildParameter(ArticleFactory.FieldNames.Title,
                                                                                      title)
                                                               
            };
            return BuildEntityFromSql(sql, parameter);
        }

        #endregion
    }
}
